﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSE.Windows.Test
{
    public partial class ChufangForm : Form
    {
        private Yishi yishi = null;
        private Yaopin yaopin = null;
        private List<Yishi> ys = new List<Yishi>();
        public Chufang chufang = null;
        public int isHospital = 1;

        public ChufangForm()
        {
            InitializeComponent();
        }

        private void ChufangForm_Load(object sender, EventArgs e)
        {
            FillKeshiLists();
            FillYishiLists();
            FillYaopinLists();
            this.comboBoxSex.SelectedIndex = 0;

            if (chufang != null)
            {
                Lingyao linyao = chufang.Lingyao;
                textBoxCount.Text = (-linyao.LingyaoNumber).ToString();
                dateTimePicker1.Value = linyao.LingyaoDate;
                textBoxPrescription.Text = linyao.LingyaoCode;

                textBoxHospitalNo.Text = chufang.HospitalNo;
                textBoxPatientCardNo.Text = chufang.PatientCardNo;
                textBoxPatientName.Text = chufang.PatientName;
                comboBoxSex.SelectedIndex = chufang.PatientSex;
                textBoxAge.Text = chufang.PatientAge.ToString();
                textBoxIllness.Text = chufang.Illness;
                textBox1BatchNo.Text = chufang.BatchNo;
                textBox1TakerCardNo.Text = chufang.TakerCardNo;
                textBoxTakerName.Text = chufang.TakerName;
                textBox1Releaser.Text = chufang.ReleaserName;
                textBox1Auditor.Text = chufang.AuditorName;
                checkBoxCancer.Checked = chufang.IsCancer == 1? true : false;
                isHospital = chufang.IsHospital;

                textBoxDoctorNo.Text = chufang.Doctor.YishiCode;
                comboBoxDoctorName.SelectedValue = chufang.Doctor.YishiID;
                comboBoxDocDept.SelectedValue = chufang.DoctorDept.KeshiID;

                comboBoxPatientDept.SelectedValue = chufang.PatientDept.KeshiID;

                textBoxDrugCode.Text = chufang.Lingyao.LingyaoYaopin.YaopinCode;
                comboBoxDrugName.SelectedValue = chufang.Lingyao.LingyaoYaopin.YaopinID;
            }

            if (isHospital == 0)
                labelHospitalNo.Text = "门诊号";
        }

        private void FillKeshiLists()
        {
            List<Keshi> ks = AnesthesiaBLL.GetAllKeshi();

            this.comboBoxPatientDept.DataSource = ks;
            this.comboBoxPatientDept.DisplayMember = "KeshiName";
            this.comboBoxPatientDept.ValueMember = "KeshiID";

            List<Keshi> ks1 = new List<Keshi>();
            ks1.AddRange(ks);
            this.comboBoxDocDept.DataSource = ks1;
            this.comboBoxDocDept.DisplayMember = "KeshiName";
            this.comboBoxDocDept.ValueMember = "KeshiID";

        }

        private void FillYishiLists()
        {
            ys = AnesthesiaBLL.GetYishiByKeshi(null);

            this.comboBoxDoctorName.DataSource = ys;
            this.comboBoxDoctorName.DisplayMember = "YishiName";
            this.comboBoxDoctorName.ValueMember = "YishiID";
        }

        private void FillYaopinLists()
        {
            List<Yaopin> yp = AnesthesiaBLL.GetAllYaopin();

            this.comboBoxDrugName.DataSource = yp;
            this.comboBoxDrugName.DisplayMember = "YaopinLongName";
            this.comboBoxDrugName.ValueMember = "YaopinID";
        }

        private void textBoxDrugCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBoxDoctorNo_TextChanged(object sender, EventArgs e)
        {
            if (textBoxDoctorNo.Text.Length == 5)
            {
                try
                {
                    Yishi y = AnesthesiaBLL.GetYishiByCode(textBoxDoctorNo.Text);
                    comboBoxDoctorName.SelectedValue = y.YishiID;
                    comboBoxDocDept.SelectedValue = y.YishiKeshiID;
                    comboBoxPatientDept.SelectedValue = y.YishiKeshiID;
                    yishi = y;
                }
                catch (Exception ex)
                {
                    yaopin = null;
                    MessageBox.Show(ex.Message, "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void textBoxDrugCode_TextChanged(object sender, EventArgs e)
        {
            if (textBoxDrugCode.Text.Length == 3)
            {
                try
                {
                    Yaopin y = AnesthesiaBLL.GetYaopinByCode(textBoxDrugCode.Text);
                    comboBoxDrugName.SelectedValue = y.YaopinID;
                    yaopin = y;
                }
                catch (Exception ex)
                {
                    yaopin = null;
                    MessageBox.Show(ex.Message, "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (chufang == null)
                chufang = new Chufang();

            if (yaopin == null)
            {
                MessageBox.Show("请选择药品！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                textBoxDrugCode.SelectAll();
                textBoxDrugCode.Focus();
                return;
            }

            if (yishi == null)
            {
                MessageBox.Show("请选择医师！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                textBoxDoctorNo.SelectAll();
                textBoxDoctorNo.Focus();
                return;
            }

            if (textBoxPatientName.Text.Length == 0)
            {
                MessageBox.Show("请选择填写患者姓名！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                textBoxPatientName.Focus();
                return;
            }

            if (this.textBoxPrescription.Text.Length == 0)
            {
                MessageBox.Show("请填写处方号！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                textBoxPrescription.Focus();
                return;
            }

            if (this.textBoxCount.Text.Length == 0)
            {
                MessageBox.Show("请填写药品数量！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                textBoxCount.Focus();
                return;
            }

            try
            {
                Lingyao linyao = chufang.Lingyao;
                linyao.LingyaoNumber = -Int32.Parse(textBoxCount.Text);
                linyao.LingyaoDate = DateTime.Parse(dateTimePicker1.Value.ToShortDateString());
                linyao.LingyaoCode = textBoxPrescription.Text;
                linyao.LingyaoYaopin = yaopin;

                chufang.Doctor = yishi;

                Keshi k = chufang.DoctorDept;
                k.KeshiID = comboBoxDocDept.SelectedValue.ToString();

                k = chufang.PatientDept;
                k.KeshiID = comboBoxPatientDept.SelectedValue.ToString();
                chufang.PatientDept = k;

                chufang.HospitalNo = textBoxHospitalNo.Text;
                chufang.PatientCardNo = textBoxPatientCardNo.Text;
                chufang.PatientName = textBoxPatientName.Text;
                chufang.PatientSex = comboBoxSex.SelectedIndex;
                if (textBoxAge.Text.Length > 0)
                    chufang.PatientAge = Int32.Parse(textBoxAge.Text);
                chufang.Illness = textBoxIllness.Text;
                chufang.BatchNo = textBox1BatchNo.Text;
                chufang.TakerCardNo = textBox1TakerCardNo.Text;
                chufang.TakerName = textBoxTakerName.Text;
                chufang.ReleaserName = textBox1Releaser.Text;
                chufang.AuditorName = textBox1Auditor.Text;
                chufang.IsCancer = checkBoxCancer.Checked ? 1 : 0;
                chufang.IsHospital = this.isHospital;
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据格式错误！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
            }
        }

        private void comboBoxDoctorName_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sel = comboBoxDoctorName.SelectedIndex;
            yishi = ys[sel];
        }
    }
}
